package pl.enigmatic.parser.markup;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import pl.enigmatic.util.Reflection;
import pl.enigmatic.util.Strings;
import pl.enigmatic.parser.xml.NodeNameInfo;
import pl.enigmatic.parser.xml.ParsingInfo;
import pl.enigmatic.parser.xml.XMLParserExtension;
import pl.enigmatic.parser.xml.NodeNameInfo.QualifiedPropertyName;


public class StaticExtension extends BasicMarkupExtension {

	public static final StaticExtension instance = new StaticExtension();

	private StaticExtension() {
		super("Static");
	}

	@Override
	public boolean parse(final Object target, final MarkupProperties props, final ParsingInfo<?> info, final XMLParserExtension handler) {
		final QualifiedPropertyName name = NodeNameInfo.getFieldName(props.getMainValue(), info.getStaticInfo());

		final String property = name.getProperty();
		if (Strings.empty(property)) { return false; }
		Class<?> clazz = name.getClazz() != null ? Reflection.classForName(name.getClazz()) : target.getClass();
		try {
			final Field field = clazz.getField(property);
			if (Modifier.isStatic(field.getModifiers())) { return handler.setParsedProperty(target, info, field.get(target)); }
		} catch (final Exception e) {}
		return false;
	}

	@Override
	public Object createInstance(MarkupProperties props, ParsingInfo<?> info) {
		// TODO Auto-generated method stub
		return null;
	}

}
